package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.home.registered;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.GenericTabPanel;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.Updatable;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.user.UserTab;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.InfoManager;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

/**
 * @author Renato Miceli
 */
public class UserInfoTabOpenerListener extends ButtonListenerAdapter implements Updatable<UserInfo> {

	private final TabPanel centerPanel;

	private UserInfo info;

	private final InfoManager<UserInfo> loggedManager;

	public UserInfoTabOpenerListener(TabPanel centerPanel, InfoManager<UserInfo> loggedManager) {
		this.centerPanel = centerPanel;
		this.loggedManager = loggedManager;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onClick(Button button, EventObject e) {

		for (Component c : this.centerPanel.getItems()) {
			if (c.getItemId().equals(UserTab.getTabId(this.info.getLogin()))) {
				this.centerPanel.activate(c.getId());
				return;
			}
		}
		UserTab userTab = new UserTab(this.info, centerPanel, loggedManager);
		this.centerPanel.add(userTab);
		this.centerPanel.activate(userTab.getId());
	}

	public void setUserLogin(String login) {
		this.info.setLogin(login);
	}

	public void updateInfo(UserInfo info) {
		this.info = info;
	}

	public void destroy() {
		
	}

	public GenericTabPanel getParentTab() {
		// TODO Auto-generated method stub
		return null;
	}
}
